package com.zj.core.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.support.annotation.RequiresApi
import android.support.design.widget.FloatingActionButton
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.view.ViewGroup
import com.zj.core.R
import org.jetbrains.anko.AnkoContext
import org.jetbrains.anko.find
import java.util.*
import android.widget.*
import com.fileutils.FontUtils
import com.zj.core.interfaces.RecordListIml
import com.zj.core.view.MeuseEditText
import org.jetbrains.anko.singleLine
import com.zj.core.ui.RecordListItemUi

class RecordListAdapter(context: Context, detailList: ArrayList<ArrayList<String>>) :
    RecyclerView.Adapter<RecordListAdapter.RecordListHolder>() {
    private var mDetailList: ArrayList<ArrayList<String>>? = detailList
    private var mContext: Context? = context

    private var mlistener:RecordListIml?=null
    private var mAddClick: View.OnClickListener? = null
    private var mTextNameWatcher: TextWatcher? = null
    private var mTextValueWatcher: TextWatcher? = null
    private var textSize:Int?=14

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecordListHolder {
        return RecordListHolder(RecordListItemUi().createView(AnkoContext.create(parent.context, parent)))
    }

    @SuppressLint("RestrictedApi")
    override fun onBindViewHolder(holder: RecordListHolder, position: Int) {
        with(holder){
            if (recordItemNameTv?.getTag(R.id.record_name_tag) != null
                && recordItemNameTv?.getTag(R.id.record_name_tag) is TextWatcher) {
                recordItemNameTv?.removeTextChangedListener(recordItemNameTv?.getTag(R.id.record_name_tag) as TextWatcher)
            }

            if (recordItemTv?.getTag(R.id.record_value_tag) != null
                && recordItemTv?.getTag(R.id.record_value_tag) is TextWatcher) {
                recordItemTv?.removeTextChangedListener(recordItemTv?.getTag(R.id.record_value_tag) as TextWatcher)
            }

            if (position == mDetailList!!.size) {                              //最后一个是添加按钮
                recordItemView?.visibility = View.GONE
                recordActionBu?.visibility = View.VISIBLE
                recordDeleteBu?.visibility = View.GONE
                recordActionBu?.setOnClickListener(mAddClick)
            } else {
                val mArrItem = mDetailList!![position]
                if (position == 0) {                         //时间项
                    recordDeleteBu?.visibility = View.GONE
                    recordDownImg?.visibility = View.GONE
                    recordItemNameTv?.isFocusable = false
                    recordItemNameTv?.isFocusableInTouchMode = false
                    recordItemTv?.isFocusable = false
                    recordItemTv?.isFocusableInTouchMode = false
                    recordItemTv?.singleLine = true
                    recordItemTv?.setOnClickListener{
                        mlistener?.showTimeDialog()
                    }
                    recordItemNameTv?.setOnClickListener{
                        mlistener?.showTimeDialog()
                    }
                    recordItemView?.setOnClickListener {
                        mlistener?.showTimeDialog()
                    }
                } else if (position == 1) {                   //表单项
                    recordDeleteBu?.visibility = View.GONE
                    recordDownImg?.visibility = View.VISIBLE
                    recordItemNameTv?.isFocusable = false
                    recordItemNameTv?.isFocusableInTouchMode = false
                    recordItemTv?.isFocusable = false
                    recordItemTv?.isFocusableInTouchMode = false
                    recordItemTv?.singleLine = true
                    recordItemTv?.setOnClickListener{
                        mlistener?.recheckClick(this.recordItemTv?.text.toString())
                    }
                    recordItemNameTv?.setOnClickListener{
                        mlistener?.recheckClick(this.recordItemTv?.text.toString())
                    }
                    recordItemView?.setOnClickListener {
                        mlistener?.recheckClick(this.recordItemTv?.text.toString())
                    }
                    recordDownImg?.setOnClickListener {
                        mlistener?.recheckClick(this.recordItemTv?.text.toString())
                    }
                } else {
                    mTextNameWatcher = object : TextWatcher {
                        override fun afterTextChanged(s: Editable?) {}
                        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
                        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                            mlistener?.editTextChange(0, s.toString(),recordItemNameTv?.tag as Int)
                        }
                    }

                    mTextValueWatcher = object : TextWatcher {
                        override fun afterTextChanged(s: Editable?) {}
                        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
                        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                            showDownImg(holder, mArrItem, position)
                            mlistener?.editTextChange(1, s.toString(), recordItemNameTv?.tag as Int)
                        }
                    }

                    recordDeleteBu?.visibility = View.VISIBLE
                    recordDownImg?.visibility = View.INVISIBLE
                    recordItemNameTv?.isFocusable = true
                    recordItemNameTv?.isFocusableInTouchMode = true
                    recordItemTv?.isFocusable = true
                    recordItemTv?.isFocusableInTouchMode = true
                    recordItemNameTv?.setTag(R.id.record_name_tag, mTextNameWatcher)
                    recordItemTv?.setTag(R.id.record_value_tag, mTextValueWatcher)

                    recordItemNameTv?.addTextChangedListener(mTextNameWatcher)
                    recordItemTv?.addTextChangedListener(mTextValueWatcher)

                    recordDeleteBu?.setOnClickListener {
                        mlistener?.deleteClick(position)
                    }
                    recordDownImg?.setOnClickListener {
                        mlistener?.showTemplateDialog(position,mArrItem)
                    }


                    recordItemTv?.setOnClickListener(null)
                    recordItemNameTv?.setOnClickListener(null)
                    recordItemView?.setOnClickListener(null)
                }
                recordItemNameTv?.tag = position
                recordItemTv?.tag = position
                recordActionBu?.visibility = View.GONE
                recordItemView?.visibility = View.VISIBLE
                recordItemNameTv?.setText(mArrItem[0])

                recordItemNameTv?.textSize= textSize?.toFloat()!!
                recordItemTv?.textSize=textSize?.toFloat()!!
                recordItemTv?.setText(mArrItem[1])
            }
        }
    }

    private fun showDownImg(holder: RecordListHolder, mArrItem: ArrayList<String>, position: Int) {
        if(mArrItem[0].contains("模板")){
            holder.recordDownImg?.visibility = View.VISIBLE
        }else{
            if (FontUtils.meauseWidthForText(mContext, mArrItem[1]) > holder.recordItemTv!!.getMeuseWidth()) {
                holder.recordDownImg?.visibility = View.VISIBLE
            } else {
                holder.recordDownImg?.visibility = View.INVISIBLE
            }
        }
    }

    override fun getItemCount(): Int {
        return mDetailList!!.size + 1
    }

    inner class RecordListHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var recordItemTv: MeuseEditText? = itemView.find(R.id.record_item_tv)
        var recordItemNameTv: EditText? = itemView.find(R.id.record_item_name_tv)
        var recordDownImg: ImageView? = itemView.find(R.id.record_item_down_img)
        var recordItemView: RelativeLayout? = itemView.find(R.id.record_item_parent_view)
        var recordActionBu: FloatingActionButton? = itemView.find(R.id.record_item_float_bu)
        var recordDeleteBu: Button? = itemView.find(R.id.record_item_delete_bu)
    }

    //设置Item字体大小
    fun setItemTextSize(size: Int?){
        this.textSize=size
    }

    /**
     * 添加一条数据
     */
    fun setAddClickListener(addClick: View.OnClickListener) {
        this.mAddClick = addClick
    }

    fun setRecordListListener(listener: RecordListIml){
        mlistener=listener
    }
}